package jt.classic.system.workflow;

import jt.classic.app.util.ListRange;

import jt.classic.system.ISystem;
import jt.classic.system.SimpleAction;
import jt.classic.system.database.SystemDataSource;
import jt.classic.system.user.IUser;

import net.sf.json.JSONObject;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.limp.mine.Controller;
import org.limp.mine.DataTrimmer;
import org.limp.mine.DateTrimmer;
import org.limp.mine.Label;

import java.sql.Connection;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 流程统计控制器
 *
 * @author mido
 *
 */
public class WFlowStatAction extends SimpleAction {
    /**
    * 待办统计(返回json数据)
    *
    * @param mapping
    * @param form
    * @param request
    * @param response
    * @return
    * @throws Exception
    */
    public ActionForward pending4Json(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response)
        throws Exception {
        Connection conn = null;

        IUser user = ISystem.getSessionUser(request);

        String sqlwhere =
            " WHERE (WF_ACTIVITY.A_STATE = 0 OR WF_ACTIVITY.A_STATE = 1 OR WF_ACTIVITY.A_STATE = -1) " +
            "AND WF_ACTIVITY.A_TYPE = 0 AND WF_INSTANCE.INS_STATE = 0 AND A.USER_ID = '" +
            user.id() + "'";

        String sql =
            "SELECT COUNT(WF_INSTANCE.INS_ID) AS CT, WF_INSTANCE.CG_ID FROM WF_ACTIVITY LEFT JOIN WF_INSTANCE ON WF_ACTIVITY.INS_ID = " +
            "WF_INSTANCE.INS_ID LEFT JOIN WF_EXECUTOR A ON WF_ACTIVITY.A_ID = A.A_ID LEFT JOIN WF_EXECUTOR B ON WF_ACTIVITY.A_LASTID = B.A_ID " +
            sqlwhere + " GROUP BY WF_INSTANCE.CG_ID";

        try {
            SystemDataSource ds = SystemDataSource.getInstance();
            conn = ds.getConnection();

            DataTrimmer trimmer = new DataTrimmer(conn);

            List resultList = trimmer.searchMultiData(sql);

            if ((resultList != null) && !resultList.isEmpty()) {
                WorkflowManager manager = new WorkflowManager(conn);

                for (int i = 0; i < resultList.size(); i++) {
                    Map info = (Map) resultList.get(i);
                    String id = (String) info.get("CG_ID");

                    WFlow flow = manager.getWFlowById(id);

                    info.put("CG_NAME", flow.iname());
                    info.put("CG_KEY", flow.iobject().ikey());
                }
            }

            ListRange lr = new ListRange();
            lr.setTotalSize(resultList.size());
            lr.setList(resultList);
            lr.setSuccess(true);

            request.setAttribute("result", JSONObject.fromObject(lr).toString());
        } catch (Exception ex) {
            request.setAttribute("result",
                JSONObject.fromObject(new ListRange()).toString());
            ISystem.catchException(request, this, ex);
        } finally {
            SystemDataSource.closeConnection(conn);
        }

        return mapping.findForward("result");
    }
}
